import pandas as pd

file_path = "C:\Machine Learning\BERT\DCOILWTICO.csv"
df = pd.read_csv(file_path)

print(df.head())
print(df.dtypes)

# 将 'DCOILWTICO' 列转换为数值类型
df['DCOILWTICO'] = pd.to_numeric(df['DCOILWTICO'], errors='coerce')

df['DCOILWTICO'] = df['DCOILWTICO'].fillna(df['DCOILWTICO'].rolling(window=3,min_periods=1,center=True).median()).round(2)


cleaned_file_path = "C:\Machine Learning\BERT\cleaned_DCOILWTICO.csv"
df.to_csv(cleaned_file_path, index=False)

print("缺失值已用上一行和下一行的中值填充，并保存到新的 CSV 文件:", cleaned_file_path)